var express = require('express');
var router = express.Router();
var request = require('request');
var tokenDao = require('./tokenDao');
var userDao = require('../dao/userDao');
var response = require('../dao/response');




// sign up
router.post('/reg', (req, res) => {

    console.log(req.body);
    // res.json({success:true});

    if (!req.body) {
        res.json({ success: false, info_massage: '缺少参数' });
    } else {

        userDao.reg(req.body, res);
    }

});

// sign in
router.post('/login', (req, res) => {

    console.log(req.body);
    // res.json({success:true});

    if (!req.body) {
        res.json({ success: false, info_massage: '缺少参数' });
    } else {

        userDao.login(req.body, res);
    }

});

// loginout
router.get('/loginOut', (req, res) => {

    var token = req.get('token');

    tokenDao.destroy(token);

    res.clearCookie('token');

    res.json({ success: true, data: {} });

});

// 用户token验证是否已过期
router.get('/verify', (req, res) => {

    var token = req.get('token');

    var userId = tokenDao.verify(token);

    if (!userId) {
        res.json({ success: false, msg: 'token 过期' });
    } else {
        res.json({ success: true, msg: '用户有效' });
    }
});

// 获取用户信息
router.get('/getUserInfo', (req, res) => {

    // res.json({success:true});
    var token = req.get('token');

    var userId = tokenDao.verify(token);

    userDao.getUserInfo(userId, res);
});


module.exports = router;